package com.labichaoka.chaoka.ui.fetch;

import com.labichaoka.chaoka.base.MyApplication;
import com.labichaoka.chaoka.entity.BaseResponse;
import com.labichaoka.chaoka.entity.FetchMoneyRequest;
import com.labichaoka.chaoka.entity.FetchMoneyResponse;
import com.labichaoka.chaoka.entity.GetWithdrawInfo;
import com.labichaoka.chaoka.entity.IsOpenAcountRequest;
import com.labichaoka.chaoka.entity.IsOpenAcountResponse;
import com.labichaoka.chaoka.entity.LimitStatusResponse;
import com.labichaoka.chaoka.net.BaseSubscriber;
import com.labichaoka.chaoka.net.DataManager;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by ruan on 2018/8/13.
 */

public class FetchMoneyInteractorImpl implements FetchMoneyInteractor {
    private DataManager manager = new DataManager(MyApplication.mContext);
    private CompositeSubscription mCompositeSubscription = new CompositeSubscription();
    @Override
    public void getQuotaInfo(OnFinishedListener listener) {
        mCompositeSubscription.add(manager.limitActStatus()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<LimitStatusResponse>() {

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        listener.onFailed();
                    }

                    @Override
                    public void onNext(LimitStatusResponse response) {
                        super.onNext(response);
                        listener.onSuccessed(response);
                    }
                })
        );
    }

    @Override
    public void commit(FetchMoneyRequest request, OnFinishedListener listener) {
        mCompositeSubscription.add(manager.fetchMoney(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<FetchMoneyResponse>() {

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        listener.onCommitFailed();
                    }

                    @Override
                    public void onNext(FetchMoneyResponse response) {
                        super.onNext(response);
                        listener.onCommitSuccessed(response);
                    }
                })
        );
    }

    @Override
    public void isOpenAcount(IsOpenAcountRequest request,OnFinishedListener listener) {
        mCompositeSubscription.add(manager.isOpenAcount(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<IsOpenAcountResponse>() {

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        listener.onIsOpenAcountFailed();
                    }

                    @Override
                    public void onNext(IsOpenAcountResponse response) {
                        super.onNext(response);
                        listener.onIsOpenAcountSucc(response);
                    }
                })
        );
    }


}
